<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Exception;


class LoginController extends Controller
{
    public function login(){
        
        return view('login');
    }

    //修改页面
    public function member_edit(){
        $member = Db::table('admin')->where('id','=',Session::get('session_id'))->first();

        return view('member',['account'=>$member->account]);
    }

    //修改登录密码
    public function login_edit_pwd(Request $request){
    	$input = $request->input();
        if(empty($input['password']))throw new Exception("请填写修改密码！");
        
    	DB::table('admin')->where('id','=',Session::get('session_id'))->update(['pwd'=>Hash::make($input['password'])]);

        return response()->json([
            'status' => 1,
            'result' => [],
            'message' => '修改成功！'
        ]);
    }

    public function login_do(Request $request){
    	$input = $request->input();

    	// 登录验证
		$result = DB::table('admin')->where('account','=',$input['username'])->first();

		if(!Hash::check($input['password'], $result->pwd))throw new Exception("密码错误或者账号错误!");

		// 用户信息存入 Session
		\Session::put('session_id', $result->id);	
        \Session::put('session_level', $result->level);   

		return response()->json([
            'status' => 1,
            'result' => [],
            'message' => '访问成功！'
        ]);
    }

    //退出登录
    public function login_up(){
    	\Session::forget('session_id');	

    	return redirect('/login');
    }
}
